import unittest
import udunits2
import math

class ExceptionTestCase(unittest.TestCase):
    def runTest(self):
        try:
            udunits2.Units.AreConvertible('m', 'foo')
        except RuntimeError:
            pass
        else:
            self.fail('Did not see RuntimeError')

        try:
            udunits2.Units.GetConverter('m', 'foo')
        except RuntimeError:
            pass
        else:
            self.fail('Did not see RuntimeError')

class IsTimeTestCase(unittest.TestCase):
    def runTest(self):
        self.assertTrue(udunits2.Units.IsTime('seconds'))
        self.assertTrue(udunits2.Units.IsTime('days since 1950-01-01'))
        self.assertFalse(udunits2.Units.IsTime('m'))
        self.assertFalse(udunits2.Units.IsTime('db'))

class AreConvertibleTestCase(unittest.TestCase):
    def runTest(self):
        self.assertTrue(udunits2.Units.AreConvertible('m', 'cm'))
        self.assertTrue(udunits2.Units.AreConvertible('m', 'km'))
        self.assertFalse(udunits2.Units.AreConvertible('m', 'dB'))

class GetConverterTestCase(unittest.TestCase):
    def runTest(self):
        self.assertEquals((0.0, 100.0), udunits2.Units.GetConverter('m', 'cm'))
        self.assertEquals((0.0, 0.01), udunits2.Units.GetConverter('cm', 'm'))
        self.assertEquals((-7305.0, 1.0),
            udunits2.Units.GetConverter('day since 1950-01-01 00:00:00+0',
                                        'day since 1970-01-01 00:00:00+0'))
        self.assertEquals((0.0, math.pi/180.0),
                          udunits2.Units.GetConverter('degree', 'radian'))

if __name__ == '__main__':
    unittest.main()
